var t = require("../../../utils/function.js"), e = require("../../../api.js"), a = require("../../../utils/qqmap-wx.js"), o = getApp(), i = wx.getStorageSync("MAP_KEY") || "PNIBZ-QF5R2-XSGUL-CGVXI-5J4GZ-VNFNX", n = new a({
    key: i
});

Page({
    data: {
        travel_type:'',
        typeIdx: 3,
        showOnePrice: "0",
        showPrice: !1,
        driverPosition: { 
            lat: 0,
            lng: 0
        },
        fenxiaoId: "",
        authStatus: !0,
        is_GZH: !1,
        canIUse: wx.canIUse("button.open-type.getUserInfo"),
        img: t.userImg,
        markers: {},
        polyline: [ {
            points: [],
            color: "#0299de",
            width: 9,
            dottedLine: !1,
            arrowLine: !0
        } ],
        startAddress: null,
        endAddress: null,
        longitude: null,
        latitude: null,
        orderOrMore: !0,
        driverCount: -1,
        pageRight: 0,
        myShow: !0,
        zuoyou: "icon-zuoyou",
        avatar: "../../../images/user/icon-07.png",
        buttonText: "登录",
        username: "未登录",
        orderGoing: !1,
        showTimeContainer: !1,
        hours: [],
        minute: [],
        homeHandlerShow: !0,
        about_time: [],
        phone: "",
        make: {},
        Hmap: "",
        left_width: 750,
        is_distance: !1,
        price: "0.00",
        show_alert_info: !1,
        userAddress: {
            location: {
                lat: "34.259430",
                lng: "108.947040"
            }
        },
        isReadProtocol: !0,
        get_order: wx.getStorageSync("get_order"),
        show_bind_phone: !1,
        scale: 15,
        isAddressText: !0,
        order_url: [ "/pages/driverOrder/orderedStatus/orderedStatus", "/pages/driverOrder/orderGoing/orderGoing", "/pages/driverOrder/bill/bill", "/pages/driverOrder/orderDetail/orderDetail", "/pages/driverOrder/orderedStatus/orderedStatus" ]
    },
    onLoad: function(t) {
        var e = this;
        this.setData({
            travel_type: o.travel_type
        })
        wx.getLocation({
            type: "gcj02",
            success: function(t) {
                e.data.driverPosition.lat = t.latitude, e.data.driverPosition.lng = t.longitude, 
                e.addressName(t.latitude + "," + t.longitude);
            }
        }), e.getBasis();
    },
    radioChange: function(t) {
        this.data.typeIdx = t.detail.value;
        var e = 5 == t.detail.value;
        this.setData({
            showPrice: e
        });
    },
    getBasis: function() {
        var t = this;
        o.request({
            url: e.user.payWay,
            method: "post",
            data: {},
            success: function(e) {
                console.log(e);
                1 == e.code && t.setData({
                    showOnePrice: e.data.open_one_price
                });
            }
        });
    },
    changePriceStatus: function() {
        this.setData({
            showPrice: !this.data.showPrice
        });
    },
    addressName: function(t) {
        var e = this;
        wx.request({
            url: "https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1",
            data: {
                key: i,
                location: t
            },
            method: "GET",
            success: function(t) {
                
                var a = e.startAddress, o = t.data.result;
                console.log(o)
                a = {
                    title: o.pois[0].title,
                    address: o.pois[0].address,
                    location: {
                        lat: o.pois[0].location.lat,
                        lng: o.pois[0].location.lng
                    }
                }, e.setData({
                    startAddress: a,
                    isAddressText: !0
                });
            },
            fail: function() {},
            complete: function() {}
        });
    },
    isRead: function(t) {
        var e = this.data.isReadProtocol;
        this.setData({
            isReadProtocol: !e
        });
    },
    gotoNews: function() {
        wx.navigateTo({
            url: "/pages/user/news/news"
        });
    },
    clearAddressAndInfo: function() {
        var e = this;
        e.setData({
            startAddress: null,
            endAddress: null,
            make: {},
            polyline: [ {
                points: [],
                color: "#0299de",
                width: 9,
                dottedLine: !1,
                arrowLine: !0
            } ],
            is_distance: !1,
            price: "0.00",
            show_alert_info: !1,
            about_time: [],
            driverCount: -1
        }), t.getLatLntHandler(function(t) {
            e.setData({
                startAddress: t,
                userAddress: t
            });
        }), e.get_line_and_count_distance();
    },
    controltap: function(e) {
        var a = this, o = this;
        1 === e.controlId ? (o.clearAddressAndInfo(), wx.navigateTo({
            url: "/pages/map/my/my"
        })) : t.getLatLntHandler(function(t) {
            a.setData({
                userAddress: t,
                startAddress: t
            });
        });
    },
    openMyPageHandler: function() {
        var t = this.data, e = (t.pageRight, t.myShow);
        e ? this.setData({
            pageRight: 620,
            left_width: 130,
            myShow: !e,
            controls: [ {
                id: 1,
                iconPath: "../../../images/user/icon-33.png",
                position: {
                    top: 144,
                    left: 10,
                    width: 41,
                    height: 41
                },
                clickable: !0
            } ],
            homeHandlerShow: !1
        }) : (this.setData({
            pageRight: 0,
            left_width: 750,
            myShow: !e,
            controls: [ {
                id: 1,
                iconPath: "../../../images/user/icon-22.png",
                position: {
                    left: 10,
                    top: 10,
                    width: 50,
                    height: 50
                },
                clickable: !0
            }, {
                id: 2,
                iconPath: "../../../images/user/icon-30.png",
                position: {
                    top: 240,
                    left: 10,
                    width: 50,
                    height: 50
                },
                clickable: !0
            } ],
            homeHandlerShow: !0
        }), this.changeIcon());
    },
    callPhoneHandler: function() {
        t.intoPageHandler("../../order/helpFriendDownloadOrder/helpFriendDownloadOrder");
    },
    getMoreDriverHandler: function() {
        this.setData({
            orderOrMore: !this.data.orderOrMore
        });
    },
    bindChange: function(t) {
        this.setData({
            driverCount: t.detail.value[0] + 1
        });
    },
    getDriverCountHandler: function() {
        this.setData({
            orderOrMore: !this.data.orderOrMore
        });
    },
    create_order: function() {
        var a = this;
        wx.showLoading({});
        var i = "" === wx.getStorageSync("relative_people") ? wx.getStorageSync("user") : wx.getStorageSync("relative_people");
        o.request({
            url: e.order.place_order,
            method: "post",
            data: {
                startAddress: JSON.stringify(this.data.startAddress),
                endAddress: JSON.stringify(this.data.endAddress),
                driver_count: -1 === this.data.driverCount ? 1 : this.data.driverCount,
                passenger_phone: i.phone,
                passenger_name: i.name,
                paid: i.replace_pay,
                type: i.type,
                about_time: this.data.about_time,
                hours: this.data.hours,
                minute: this.data.minute,
                dat: this.data.date,
                travel_type : o.travel_type
            },
            success: function(i) {
                wx.hideLoading(), 1 == i.code ? (wx.canIUse("requestSubscribeMessage") && o.request({
                    url: e.message.getTmplIds,
                    method: "GET",
                    data: {
                        type: 1
                    },
                    success: function(t) {
                        wx.requestSubscribeMessage({
                            tmplIds: t.data,
                            complete: function(t) {}
                        });
                    }
                }), i.data.orderid ? (wx.setStorageSync("orderid", i.data.orderid), a.clearAddressAndInfo(), 
                o.driver(), wx.navigateTo({
                    url: "../orderAndDriverGoing/orderAndDriverGoing?order_no=" + i.data.orderid
                })) : t.showToast(i.msg, "none")) : a.open_alert_info(i.msg);
            }
        });
    },
    open_alert_info: function(t) {
        var e = this;
        this.setData({
            show_alert_info: !0,
            alert_info: t
        }), setTimeout(function() {
            e.setData({
                show_alert_info: !1
            });
        }, 2e3);
    },
    callServerHandler: function(a) {
        wx.getStorageSync("access_token"), wx.getStorageSync("user_info");
        var i = this.data.endAddress, n = this.data.startAddress, s = this.userPhone, r = this.userprice, d = this.data.typeIdx, c = this;
        return i || (i = n), s ? this.data.showPrice && !r ? (t.showToast("请输入金额", "none"), 
        !1) : r && r <= 0 ? (t.showToast("一口价金额需大于0", "none"), !1) : (wx.showLoading({
            title: "请稍侯"
        }), void o.request({
            url: e.order.orderSelf,
            method: "post",
            data: {
                startAddress: JSON.stringify(n),
                endAddress: JSON.stringify(i),
                driver_count: 1,
                user_phone: s,
                onePrice: r || 0,
                origin: 0,
                type: d
            },
            success: function(e) {
                wx.hideLoading(), wx.showToast({
                    title: e.msg,
                    icon: "none"
                }), 1 == e.code ? c.setData({
                    phone: ""
                }, function() {
                    t.intoPageHandler("/pages/driverOrder/orderedStatus/orderedStatus?orderstart=1&order_no=" + e.data.order.order_no);
                }) : 1001 == e.code && setTimeout(function() {
                    if (4 == e.data.is_service) return t.intoPageHandler(c.data.order_url[e.data.is_service] + "?orderstart=1&order_no=" + e.data.order_no), 
                    !1;
                    t.intoPageHandler(c.data.order_url[e.data.is_service] + "?order_no=" + e.data.order_no);
                }, 800);
            }
        })) : (t.showToast("请输入手机号", "none"), !1);
    },
    orderGoingHandler: function() {
        t.showToast("您还没有进行中的订单", "none");
    },
    mark_icon: function() {
        var t = this, e = this.data.startAddress, a = this.data.endAddress, o = [];
        if (e) {
            o[0] = {
                id: 0,
                iconPath: "../../../images/user/icon-44.png",
                latitude: e.location.lat,
                longitude: e.location.lng,
                width: 40,
                height: 40
            };
            var i = {
                location: {
                    lat: e.location.lat,
                    lng: e.location.lng
                }
            };
        }
        a && (o[1] = {
            id: 1,
            iconPath: "/images/user/icon-43.png",
            latitude: a.location.lat,
            longitude: a.location.lng,
            width: 40,
            height: 40
        }), e && a && this.setData({
            userAddress: i,
            scale: 13
        }), t.setData({
            make: o
        });
    },
    onShow: function() {},
    getPhone: function(t) {
        this.userPhone = t.detail.value;
    },
    getPrice: function(t) {
        this.userprice = t.detail.value;
    },
    getSettings: function() {
        wx.getSetting({
            success: function(t) {
                void 0 != t.authSetting["scope.userLocation"] && 1 != t.authSetting["scope.userLocation"] ? wx.showModal({
                    title: "请求授权当前位置",
                    content: "需要获取您的地理位置，请确认授权",
                    success: function(t) {
                        t.cancel ? wx.showToast({
                            title: "拒绝授权",
                            icon: "none",
                            duration: 1e3
                        }) : t.confirm && wx.openSetting({
                            success: function(t) {
                                1 == t.authSetting["scope.userLocation"] ? (wx.showToast({
                                    title: "授权成功",
                                    icon: "success",
                                    duration: 1e3
                                }), wx.getLocation({
                                    success: function(t) {}
                                })) : wx.showToast({
                                    title: "授权失败",
                                    icon: "none",
                                    duration: 1e3
                                });
                            }
                        });
                    }
                }) : (t.authSetting["scope.userLocation"], wx.getLocation({
                    success: function(t) {}
                }));
            }
        });
    },
    get_driver_lat_lng: function() {
        var t = this, a = this.data.startAddress, i = this.data.endAddress;
        o.request({
            url: e.user.getDriverPosition,
            data: {
                lat: t.data.driverPosition.lat,
                lng: t.data.driverPosition.lng
            },
            success: function(e) {
                for (var o = [], n = e.data, s = 0; s < n.length; s++) null != n[s].location && o.push({
                    iconPath: "../../../images/user/icon-50.png",
                    id: n[s].id,
                    latitude: n[s].location.lat,
                    longitude: n[s].location.lng,
                    width: 60,
                    height: 60,
                    callout: {
                        content: n[s].real_name + "   " + n[s].distance + "km",
                        bgColor: "#666",
                        color: "#fff",
                        padding: "6",
                        display: "ALWAYS",
                        borderRadius: "6"
                    }
                });
                if (a) {
                    o.push({
                        id: 0,
                        iconPath: "../../../images/user/icon-44.png",
                        latitude: a.location.lat,
                        longitude: a.location.lng,
                        width: 40,
                        height: 40
                    });
                    a.location.lat, a.location.lng;
                }
                i && o.push({
                    id: 1,
                    iconPath: "/images/user/icon-43.png",
                    latitude: i.location.lat,
                    longitude: i.location.lng,
                    width: 40,
                    height: 40
                }), t.setData({
                    make: o
                });
            }
        });
    },
    get_line_and_count_distance: function(t) {
        var a = this, i = this.data.startAddress, n = this.data.endAddress;
        if (i && n) {
            if (this.getPolyline(i, n), !i && !n) return;

            console.log( e.order_forecast.order_forecast)
            o.request({
                url: e.order_forecast.order_forecast,
                method: "post",
                data: {
                    start: JSON.stringify(n),
                    end: JSON.stringify(i),
                    dat: a.data.date.join(","),
                    hours: a.data.hours.join(","),
                    minute: a.data.minute.join(","),
                    about_time: a.data.about_time.join(","),
                    travel_type : o.travel_type
                },
                success: function(t) {
                    a.setData({
                        price: t.data.price,
                        distance: t.data.actual_km,
                        is_distance: !0,
                    });
                }
            });
        }
    },
    getPolyline: function(t, e) {
        var a = this, o = this.data.polyline, n = t.location.lat + "," + t.location.lng + "&to=" + e.location.lat + "," + e.location.lng ,p = '&policy=LEAST_DISTANCE';
        wx.request({
            url: "https://apis.map.qq.com/ws/direction/v1/driving/?from=" + n + "&key=" + i + p ,
            header: {
                "content-type": "application/json"
            },
            success: function(t) {
                if (384 != t.data.status) {
                    for (var e = t.data.result.routes[0].polyline, i = 2; i < e.length; i++) e[i] = e[i - 2] + e[i] / 1e6;
                    for (var n = [], s = 0; s < e.length; s += 2) n[n.length] = {
                        longitude: e[s + 1],
                        latitude: e[s]
                    };
                    o[0].points = n, a.setData({
                        polyline: o
                    });
                }
            },
            fail: function() {}
        });
    },
    get_distance: function(t, e, a) {
        n.calculateDistance({
            from: {
                latitude: t.location.lat,
                longitude: t.location.lng
            },
            to: [ {
                latitude: e.location.lat,
                longitude: e.location.lng
            } ],
            success: function(t) {
                a({
                    distance: t.result
                });
            },
            fail: function(t) {
                a(t);
            },
            complete: function(t) {}
        });
    },
    loginHandler: function(a) {
        var i = this;
        "登录" === this.data.buttonText ? t.loginHandler(function(t) {
            i.getUserInfo();
        }) : (o.request({
            url: e.user.loginout,
            method: "post",
            data: {},
            success: function(t) {
                1 == t.code && (wx.removeStorageSync("userMessage"), wx.removeStorageSync("phonep"), 
                wx.removeStorageSync("access_token"), wx.removeStorage({
                    key: "access_token"
                }), wx.removeStorageSync("uid")), wx.showToast({
                    title: t.msg,
                    duration: 2e3
                });
            }
        }), this.setData({
            avatar: "../../../images/user/icon-07.png",
            username: "未登录",
            buttonText: "登录",
            phone: ""
        }));
    },
    getAuthorizationImg: function() {
        wx.removeStorageSync("access_token");
        var t = this;
        wx.showLoading(), this.setData({
            authStatus: !1
        }), o.request({
            url: e.share.authorization,
            method: "post",
            data: {},
            success: function(e) {
                wx.hideLoading(), t.setData({
                    authorize: e.data.image
                });
            }
        });
    },
    getUserInfo: function(t) {
        var a = this;
        this.getSettings(), wx.showLoading({}), wx.login({
            success: function(t) {
                t.code && wx.getUserProfile({
                    success: function(i) {
                        console.log(i);
                        wx.hideLoading(), o.request({
                            url: e.user.login,
                            method: "post",
                            data: {
                                code: t.code,
                                iv: i.iv,
                                encryptedData: i.encryptedData,
                                id: a.data.fenxiaoId
                            },
                            success: function(t) {
                                if (1 == t.code) {
                                    t.data.is_binding;
                                    wx.showToast({
                                        title: "登录成功",
                                        icon: "success",
                                        duration: 2e3
                                    }), wx.setStorageSync("user_info", t.data), wx.setStorageSync("uid", t.data.id), 
                                    a.setData({
                                        authStatus: !0,
                                        is_GZH: !0
                                    }), wx.setStorageSync("access_token", t.data.access_token), a.order_check();
                                } else wx.removeStorage({
                                    key: "access_token"
                                }), wx.removeStorage({
                                    key: "uid"
                                }), wx.showToast({
                                    title: "登录失败",
                                    icon: "none",
                                    duration: 2e3
                                });
                            }
                        });
                    }
                });
            }
        });
    },
    intoPageHandler: function(e) {
        t.intoPageHandler(e.currentTarget.dataset.skip);
    },
    startAddress: function() {
        var t = this;
        wx.chooseLocation({
            success: function(e) {
                t.setData({
                    startAddress: {
                        title: e.name,
                        address: e.address,
                        location: {
                            lat: e.latitude,
                            lng: e.longitude
                        }
                    }
                }), t.mark_icon(), t.get_line_and_count_distance();
            }
        });
    },
    endAddress: function() {
        var t = this;
        wx.chooseLocation({
            success: function(e) {
                t.setData({
                    endAddress: {
                        title: e.name,
                        address: e.address,
                        location: {
                            lat: e.latitude,
                            lng: e.longitude
                        }
                    }
                }), t.get_line_and_count_distance(), t.mark_icon();
            }
        });
    },
    relstiveServerHandler: function() {
        t.callPhoneHandler(wx.getStorageSync("serverphone"));
    },
    selectTimeShowTimeHandler: function() {
        this.setData({
            showTimeContainer: !0,
            homeHandlerShow: !1
        }), this.selectTimeHandler(0, 0, 0);
    },
    getTimeHandlerCancle: function() {
        this.setData({
            showTimeContainer: !1,
            homeHandlerShow: !0
        });
    },
    getTimeHandlerOk: function() {
        var t = this.data, e = t.hours, a = t.minute;
        this.data.endAddress ? this.get_line_and_count_distance() : wx.showToast({
            title: "请选择目的地",
            icon: "none"
        }), this.setData({
            now_hours: e,
            now_minute: a,
            showTimeContainer: !1,
            homeHandlerShow: !0
        });
    },
    bindTimeChange: function(t) {
        this.setData({
            about_time: t.detail.value
        }), 0 === t.detail.value[0] && 0 === t.detail.value[1] ? this.selectTimeHandler(0, 0) : 0 === t.detail.value[0] && 0 !== t.detail.value[1] ? this.selectTimeHandler(0, t.detail.value[1]) : 0 !== t.detail.value[0] && this.selectTimeHandler(t.detail.value[0], t.detail.value[1], 0);
    },
    selectTimeHandler: function(t, e, a) {
        var o = this.data, i = o.hours, n = o.minute, s = new Date(), r = s.getHours(), d = s.getMinutes() + 15;
        if (d < 60 && i.push(r + "点"), d > 60 && (d -= 60), i.length = 0, n.length = 0, 
        0 === t && 0 === e) {
            for (l = r + 1; l < 24; l++) i.push(l + "点");
            if (0 != parseInt(d % 10)) c = 10 * parseInt(d / 10) + 10; else var c = 10 * parseInt(d / 10);
            for (l = c; l < 60; l += 10) n.push(l);
            this.setData({
                hours: i,
                minute: n
            });
        } else if (0 === t && 0 !== e) {
            i.push(r + "点");
            for (l = r + 1; l < 24; l++) i.push(l + "点");
            for (l = 0; l < 60; l += 10) n.push(l);
            this.setData({
                hours: i,
                minute: n
            });
        } else if (0 !== t) {
            for (l = 0; l < 24; l++) i.push(l + "点");
            for (var l = 0; l < 60; l += 10) n.push(l);
            this.setData({
                hours: i,
                minute: n
            });
        }
    },
    changeIcon: function() {
        var t = this, e = this.data.controls;
        wx.getSystemInfo({
            success: function(a) {
                var o = a.windowWidth / 750 * 500, i = a.windowHeight - o;
                e[1] = {
                    id: 2,
                    iconPath: "../../../images/user/icon-30.png",
                    position: {
                        top: i - 56,
                        left: 10,
                        width: 50,
                        height: 50
                    },
                    clickable: !0
                }, t.setData({
                    winPath: {
                        w: a.windowWidth,
                        h: a.windowHeight
                    },
                    map_height: i,
                    controls: e
                });
            }
        });
    },
    onUnload: function() {
        this.setData({
            end: ""
        });
    },
    onHide: function() {
        this.setData({
            end: ""
        });
    },
    formSubmit: function(t) {
        wx.getStorageSync("user_info").id;
        this.callServerHandler(null);
    },
    onShareAppMessage: function() {
        return {
            title: o.shareAppMessage().title,
            imageUrl: o.shareAppMessage().imageUrl,
            desc: o.shareAppMessage().desc,
            path: o.shareAppMessage().path
        };
    }
});